public class testwork {
    class TreeNode { int val;
     TreeNode left;
 TreeNode right;
 TreeNode(int x) { val = x; }
 }
    class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if(root==null){
                return null;
            }
            if(p==root||q==root){
                return root;
            }
            TreeNode  Treeleft=lowestCommonAncestor(root.left,p,q);
            TreeNode  Treeright=lowestCommonAncestor(root.right,p,q);
            if(Treeleft!=null&&Treeright!=null){
                return root;
            }
            else if(Treeleft==null){
                return Treeright;
            }
            else{
                return Treeleft;
            }

        }
    }
}
